﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.module
{
    public partial class Article : System.Web.UI.Page
    {

        private mamnontuoitho7.AppCode.Article _article = new mamnontuoitho7.AppCode.Article();
        protected Product _product = new Product();
        protected General _general = new General();
        protected Comment _comment = new Comment();

        protected DataTable _dt;
        protected int _pageflag;
        protected int _currentbar = 1;

        protected int _totalPages;
        protected int _totalArticleNumber;
        protected int _totalBars;

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["memberID"].ToString() != "" && (Session["role"].ToString() == "1"))
            {
                try
                {
                    //_dt = _article.GetAllArticleToUser();
                    _totalArticleNumber = _general.TotalNumberOfTable("ArticleListView").GetValueOrDefault();
                    _totalPages = _general.Divide(_totalArticleNumber, ConstantClass._pageStepArticle);
                    _totalBars = _general.Divide(_totalPages, ConstantClass._barStepArticle);

                    if (Request.QueryString["page"] != null)// khi bấm vào các ô số
                    {

                        _pageflag = Convert.ToInt16(Request.QueryString["page"].ToString());// trang hiện hành

                        _dt = _article.GetArticlePaged(_pageflag, ConstantClass._pageStepArticle, "3");
                    }
                    if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
                    {
                        _currentbar = 1;
                        _pageflag = 1;
                        _dt = _article.GetArticlePaged(1, ConstantClass._pageStepArticle, "3");
                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
                    {

                        _pageflag = _currentbar * ConstantClass._barStepArticle + 1;
                        _dt = _article.GetArticlePaged(_pageflag, ConstantClass._pageStepArticle, "3");
                        _currentbar += 1;
                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
                    {
                        _currentbar -= 1;
                        _pageflag = (_currentbar - 1) * ConstantClass._barStepArticle + 1;
                        _dt = _article.GetArticlePaged(_pageflag, ConstantClass._pageStepArticle, "3");

                    }

                    _beginPage = (_currentbar - 1) * ConstantClass._barStepArticle + 1;
                    _endPage = _currentbar * ConstantClass._barStepArticle;

                    if (_endPage > _totalPages)
                    {
                        _endPage = _totalPages;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorLoginPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }
    }
}